package com.codingfly.service.impl;

import com.codingfly.core.jdbc.service.impl.JPAServiceImpl;
import com.codingfly.entity.SysMenu;
import com.codingfly.repository.SysMenuRepository;
import com.codingfly.service.SysMenuService;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SysMenuServiceImpl extends JPAServiceImpl<SysMenuRepository, SysMenu, Long> implements SysMenuService {

    @Autowired
    private SysMenuRepository sysMenuRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public SysMenu findByCode(String code) {
        Specification specification = Specifications.and().eq("code", code).build();
        Optional<SysMenu> optional = sysMenuRepository.findOne(specification);
        return optional.isPresent()? optional.get():null;
    }

    @Override
    public boolean hasChildren(Long id) {
        String sql = "SELECT id FROM sys_menu WHERE parent_id=? LIMIT 1";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, id);
        return !ObjectUtils.isEmpty(list);
    }

}

